package gov.jslt.app.tool;

import java.io.IOException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import gov.jslt.app.vo.VesionV0;

public class XmlTool {
	
	public static VesionV0 GetRemoteVesionInfo(String remotePath)
			throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder dbd = dbf.newDocumentBuilder();
		URL url = new URL(remotePath);
		Document doc = dbd.parse(url.openStream());
		Element element = doc.getDocumentElement();
		NodeList childNodes = element.getChildNodes();
		VesionV0 vesionV0 = new VesionV0();
		for (int i = 0; i < childNodes.getLength(); i++) {
			if ("#text".equals(childNodes.item(i).getNodeName())) {
				continue;
			}
			if ("bbh".equals(childNodes.item(i).getNodeName())) {
				vesionV0.setBbh(childNodes.item(i).getTextContent());
			} else if ("info".equals(childNodes.item(i).getNodeName())) {
				vesionV0.setInfo(childNodes.item(i).getTextContent());
			} else if ("url".equals(childNodes.item(i).getNodeName())) {
				vesionV0.setUrl(childNodes.item(i).getTextContent());
			} else if ("upDate".equals(childNodes.item(i).getNodeName())) {
				vesionV0.setUpDate(childNodes.item(i).getTextContent());
			} else if("checkMd5".equals(childNodes.item(i).getNodeName())){
				vesionV0.setCheckMd5(childNodes.item(i).getTextContent());
			}
		}

		return vesionV0;
	}
}
